<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/30
 * @Description: 举报主表
 */

namespace App\Models;

use App\Jobs\ReportAudit;
use App\Jobs\ReportTask;
use App\Services\Aliyun\Video\VideoServices;
use Illuminate\Database\Eloquent\SoftDeletes;

class ImReport extends BaseModel
{
    protected $table = 'im_report';

    //举报者关联
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'user_id', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    //被举报者
    public function otherUser()
    {
        return $this->belongsTo(ImUsers::class, 'other_uid', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    //举报类别
    public function cate()
    {
        return $this->belongsTo(ImReportCate::class, 'cate_id', 'id')
            ->select(['id', 'cate_name']);
    }

    //处理进度
    public function process()
    {
        return $this->hasMany(ImReportProcess::class, 'report_id', 'id');
    }

    /**
     * 获取图片字段格式转换
     * @param $value
     * @return mixed
     * @author an
     * @time 2021-12-14 14:50
     */
    public function getPictureAttribute($value)
    {
        return json_decode($value, true);
    }

    /**
     * 获取图片task字段格式转换
     * @param $value
     * @return mixed
     * @author an
     * @time 2021-12-14 14:50
     */
    public function getPictureTaskAttribute($value)
    {
        return json_decode($value, true);
    }

    /**
     * 获取内容字段格式转换
     * @param $value
     * @return mixed
     * @author an
     * @time 2021-12-14 14:50
     */
    public function getContentAttribute($value)
    {
        return json_decode($value, true);
    }

    /**
     * @desc 举报聊天记录
     * @param array $input 数组
     * @param int $userId
     * @return array
     * @author zhouzhou
     * @time 2021/11/29 12:45
     */
    public function addReportChat($input, $userId = 0)
    {
        $fileAIMediaAuditJob = new VideoServices();
        $picture = $input['picture'] ?? []; // 图片url

        $pictureTaskData  = [];
        foreach ($picture as $k =>$v)
        {
            $pictureType = url_type($v);
            $pictureTaskData[] = $fileAIMediaAuditJob->submitAIMediaAuditJob($v,$pictureType - 1); //type=1提交图片  2视频  $taskId智能检测任务ID(可获取智能检查结果）
        }

        $picture = json_encode($picture);
        $pictureTask = json_encode($pictureTaskData);


        $content = $input['content'] ?? []; // 举报内容
        foreach ($content as $key =>&$val)
        {
            $type = url_type($val['content']);
            if ($type != 1)
            {
                $val['task_id'] = $fileAIMediaAuditJob->submitAIMediaAuditJob($val['content'],$type - 1); //type=1提交图片  2视频  $taskId智能检测任务ID(可获取智能检查结果）
            }
        }

        $content = json_encode($content);

        $this->type = empty($input['type']) ? 1 : 2;
        $this->user_id = $userId;
        $this->other_uid = $input['other_uid'] ?? 0;
        $this->im_gid = $input['group_id'] ?? '';
        $this->cate_id = $input['cate_id'];
        $this->picture = $picture;
        $this->picture_task = $pictureTask; // 图片的taskid
        $this->content = $content;
        $this->note = $input['note'] ?? '';

        $this->save();

        ReportAudit::dispatch(['report_id'=>$this->id]);

        return ['code' => 0, 'msg' => '举报成功'];
    }

}
